package com.irishud.hudnavi.custom_view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.TextView;

import com.irishud.hudnavi.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义高德导航光柱控件
 * Created by lucian on 16-12-11.
 */

public class TrafficStatusBarView extends TextView {

    public final static class BarItem{
        public int status;
        public int seg_len;
        public int x0;
        public int y0;
        public int mapped_width;
        public BarItem(int st,int seg){
            this.seg_len = seg;
            this.status = st;
            x0 = 0;
            y0 = 0;
        }
    }

    int good_traffic_color = Color.GREEN;
    int slow_traffic_color = Color.DKGRAY;
    int congestion_traffic_color = Color.CYAN;
    int bad_congetion_traffic_color = Color.RED;
    int finish_progress_color = Color.DKGRAY;

    int[] colors = new int[]{
            good_traffic_color,
            slow_traffic_color,
            congestion_traffic_color,
            bad_congetion_traffic_color,
    };

    int total_len = 100;
    int finish_progress = 0;
    int lineWidth = 5;
    int finishIndicatorHeight = 10;
    List<BarItem> itemList = new ArrayList<BarItem>();
    BarItem finishedItem = new BarItem(1,33);
    Drawable currentDrawble;
    Drawable destinationDrawable;

    public TrafficStatusBarView(Context context) {
        super(context);
    }

    public TrafficStatusBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        itemList.add(new BarItem(1,20));
        itemList.add(new BarItem(3,13));
        itemList.add(new BarItem(0,25));
        itemList.add(new BarItem(1,10));
        itemList.add(new BarItem(0,13));
        itemList.add(new BarItem(2,55));
        itemList.add(new BarItem(1,89));
        init(context,attrs);
    }

    public TrafficStatusBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // init(context,attrs);
    }

    public void setTotalLen(int total_len){
        this.total_len = total_len;
        invalidate();
    }

    public void setFinish_progress(int progress){
        finishedItem.seg_len = progress;
        invalidate();
    }

    public void updateBarItemList(List<BarItem> item){
        itemList.clear();
        itemList.addAll(item);
        invalidate();
    }

    private void init(Context context,AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyTrafficStatusBar);
        good_traffic_color = ta.getColor(R.styleable.MyTrafficStatusBar_good_traffic_color,Color.GREEN);
        slow_traffic_color = ta.getColor(R.styleable.MyTrafficStatusBar_slow_traffic_color,Color.BLUE);
        congestion_traffic_color = ta.getColor(R.styleable.MyTrafficStatusBar_congestion_traffic_color,Color.CYAN);
        bad_congetion_traffic_color = ta.getColor(R.styleable.MyTrafficStatusBar_bad_congestion_traffic_color,Color.RED);
        finish_progress_color = ta.getColor(R.styleable.MyTrafficStatusBar_finish_progress_color,Color.GRAY);
        destinationDrawable = ta.getDrawable(R.styleable.MyTrafficStatusBar_destination_icon);
        ta.recycle();
    }

    private void drawBarSegments(Canvas canvas, BarItem barItem){
        //horizon
        int w = getMeasuredWidth();
        int h = getMeasuredHeight();
        float barX0 = barItem.x0;
        float barY0 = h / 2;

        float barX1 = barItem.x0 + barItem.mapped_width;
        float barY1 = h / 2;

        Paint paint = new Paint();
        paint.setStrokeWidth(lineWidth);
        paint.setColor(colors[barItem.status]);
        canvas.drawLine(barX0,barY0,barX1,barY1,paint);
    }

    private void drawFinishProgress(Canvas canvas){
        int w = getWidth();
        int h = getHeight();
        finishedItem.mapped_width = (w * finishedItem.seg_len / total_len);
        finishedItem.x0 = 0;

        float barX0 = finishedItem.x0;
        float barY0 = h / 2;

        float barX1 = finishedItem.x0 + finishedItem.mapped_width;
        float barY1 = h / 2;

        Paint paint = new Paint();
        paint.setStrokeWidth(lineWidth);
        paint.setColor(colors[finishedItem.status]);
        canvas.drawLine(barX0,barY0,barX1,barY1,paint);

        // 绘制完成指示三角形
        Path path = new Path();
        PointF top = new PointF(barX1,barY1 + lineWidth);
        PointF left = new PointF(top.x -finishIndicatorHeight,top.y+finishIndicatorHeight+lineWidth);
        PointF right =  new PointF(top.x +finishIndicatorHeight,top.y+finishIndicatorHeight + lineWidth);
        path.moveTo(top.x,top.y);
        path.lineTo(left.x,left.y);
        path.lineTo(right.x,right.y);
        path.close();
        Paint pathPaint = new Paint();
        pathPaint.setColor(Color.WHITE);
        pathPaint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(2f);
        canvas.drawPath(path,pathPaint);
    }

    private void drawDestiantionIcon(Canvas canvas){
        int w = getWidth();
        int h = getHeight();
        BitmapDrawable bmp = (BitmapDrawable)destinationDrawable;
        Bitmap bitmap = bmp.getBitmap();
        Paint paint = new Paint();
        canvas.drawBitmap(bitmap,w - bitmap.getWidth()-5,h/2 - bitmap.getHeight()/2,paint);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int w = getWidth();
        int h = getHeight();
        total_len = 0;

        for (int i=0;i<itemList.size() ;i++){
            total_len += itemList.get(i).seg_len;
        }

        int loc = 0;
        for (int i=0;i<itemList.size() ;i++){
            itemList.get(i).mapped_width = (w * itemList.get(i).seg_len / total_len);
            itemList.get(i).x0 = loc;
            loc += itemList.get(i).mapped_width;
        }

        for (BarItem barItem : itemList){
            drawBarSegments(canvas,barItem);
        }

        drawFinishProgress(canvas);

        drawDestiantionIcon(canvas);
    }
}
